/**
 * Created by cjh on 17/6/19.
 */
function showDialog(message) {
    $("#progressBar").html(message);
    dialog.show();
}

function hideDialog() {
    dialog.hide();
}

function warning(msg) {
    $.toast({
        heading: '警告',
        text: msg,
        showHideTransition: 'fade',
        hideAfter: 2000,
        icon: 'warning',
        bgColor: '#ff0000',
        position: 'mid-center',
        loader: false
    })
}

function error(msg) {
    $.toast({
        heading: '警告',
        text: msg,
        showHideTransition: 'fade',
        hideAfter: 2000,
        icon: 'error',
        bgColor: '#ff0000',
        position: 'mid-center',
        loader: false
    })
}

function info(msg) {
    $.toast({
        heading: '提醒',
        text: msg,
        showHideTransition: 'fade',
        hideAfter: 5000,
        icon: 'info',
        bgColor: '#fffc00',
        textColor: "#000000",
        position: 'mid-center',
        loader: false
    })
}

function loadContentPage(page, layoutId) {
    showDialog("正在加载资源...");
    $.ajax({
        url: page,
        type: "GET",
        async: true,
        dataType: "html",
        data: {random: (new Date()).valueOf()},
        success: function (res) {
            hideDialog();
            $("#" + layoutId).html(res);
        },
        error: function (err) {
            var h = $("#" + layoutId).height();
            var div = '<div style="font-size:50px;color:#666;text-align:center;background:#0e1f2d;width:100%;height:' + h + 'px;line-height:' + h + 'px;">模块未实现</div>';
            $("#" + layoutId).html(div);
            hideDialog();
        }
    })
}

function getFileSize(size) {
    if (size < 1024.0) {
        return size + " B";
    } else if (size < (1024 * 1024.0)) {
        return Math.floor(size / 1024 + 0.5) + " K";
    } else if (size < (1024 * 1024 * 1024.0) + 0.5) {
        return Math.floor(size / (1024 * 1024) + 0.5) + " M";
    } else if (size < (1024 * 1024 * 1024 * 1024.0) + 0.5) {
        return Math.floor(size / (1024 * 1024 * 1024) + 0.5) + " G";
    } else if (size < (1024 * 1024 * 1024 * 1024 * 1024.0)) {
        return Math.floor(size / (1024 * 1024 * 1024 * 1024) + 0.5) + " T";
    }
    return size + "";
}

String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

function StringBuffer() {
    this.content = new Array;
}

StringBuffer.prototype.append = function (str) {
    this.content.push(str);
}
StringBuffer.prototype.toString = function () {
    return this.content.join("");
}
/**
 * 时间对象的格式化;
 */
Date.prototype.format = function (format) {
    /*
     * eg:format="yyyy-MM-dd hh:mm:ss";
     */
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds()
        // millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
            - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}
/** 格式化输入字符串**/
//用法: "hello{0}".format('world')；返回'hello world'
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function (s, i) {
        return args[i];
    });
}


function jSONStringify(obj) {
    if (JSON != null)
        return JSON.stringify(obj);
    else {

    }
}

function parseJSON(str) {
    /*
     if(JSON!=null)
     return JSON.parse(str);
     else {
     var obj = eval("(" + str + ")");
     return obj;
     }*/
    return JSON.parse(str);
}

function numToStr(num, pos) {
    var bb = num + "";
    var result = "";
    if (bb.indexOf(".") != -1) {
        var bs = bb.split(".");
        if (bs[1].length >= pos) {
            result = bs[0] + "." + bs[1].substring(0, pos);
        } else {
            result = bb;
        }
        return result;
    } else {
        return num + ".00";
    }
}

function isContains(str, substr) {
    return new RegExp(substr).test(str);
}

function TimeDifference(time1, time2) {
    var begin1 = time1.substr(0, 10).split("-");
    var end1 = time2.substr(0, 10).split("-");
    var date1 = new Date(begin1[1] + -+begin1[2] + -+begin1[0]);
    var date2 = new Date(end1[1] + -+end1[2] + -+end1[0]);
    //得到两个日期之间的差值m，以分钟为单位
    //Math.abs(date2-date1)计算出以毫秒为单位的差值
    //Math.abs(date2-date1)/1000得到以秒为单位的差值
    //Math.abs(date2-date1)/1000/60得到以分钟为单位的差值
    var m = parseInt(Math.abs(date2 - date1) / 1000 / 60);

    //小时数和分钟数相加得到总的分钟数
    //time1.substr(11,2)截取字符串得到时间的小时数
    //parseInt(time1.substr(11,2))*60把小时数转化成为分钟
    var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
    var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));

    //两个分钟数相减得到时间部分的差值，以分钟为单位
    var n = min2 - min1;

    //将日期和时间两个部分计算出来的差值相加，即得到两个时间相减后的分钟数
    var minutes = m + n;
    return minutes;
}

var parseColor = function (hexStr) {
    return hexStr.length === 4 ? hexStr.substr(1).split('').map(function (s) {
        return 0x11 * parseInt(s, 16);
    }) : [hexStr.substr(1, 2), hexStr.substr(3, 2), hexStr.substr(5, 2)].map(function (s) {
        return parseInt(s, 16);
    })
};
var pad = function (s) {
    return (s.length === 1) ? '0' + s : s;
};

var gradientColors = function (start, end, steps, gamma) {
    var i, j, ms, me, output = [], so = [];
    gamma = gamma || 1;
    var normalize = function (channel) {
        return Math.pow(channel / 255, gamma);
    };
    start = parseColor(start).map(normalize);
    end = parseColor(end).map(normalize);
    for (i = 0; i < steps; i++) {
        ms = i / (steps - 1);
        me = 1 - ms;
        for (j = 0; j < 3; j++) {
            so[j] = pad(Math.round(Math.pow(start[j] * me + end[j] * ms, 1 / gamma) * 255).toString(16));
        }
        output.push('#' + so.join(''));
    }
    return output;
};

function toThousands(num) {
    var result = [], counter = 0;
    num = (num || 0).toString().split('');
    for (var i = num.length - 1; i >= 0; i--) {
        counter++;
        result.unshift(num[i]);
        if (!(counter % 3) && i != 0) {
            result.unshift(',');
        }
    }
    return result.join('');
}

function doHandleMonth(month) {
    var m = month;
    if (month.toString().length == 1) {
        m = "0" + month;
    }
    return m;
}

function getDay(day) {
    var today = new Date();

    var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;

    today.setTime(targetday_milliseconds); //注意，这行是关键代码

    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = doHandleMonth(tMonth + 1);
    tDate = doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate;
}

function getClientHeight() {
    var clientHeight = 0;
    if (document.body.clientHeight && document.documentElement.clientHeight) {
        var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
    }
    else {
        var clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
    }
    return clientHeight;
}

function delHtmlTag(content) {
    var temp = content.replace(/<\/?.+?>/g, "");
    var result = temp.replace(/ /g, "").replace(/&nbsp;/g, "");
    return result;
}